<!DOCTYPE html>
<html lang="zh-cn">
  <head>
    <meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="generator" content="Hugo 0.82.0" />


<title>R 包 git2r 了解 - 宇飞的世界</title>
<meta property="og:title" content="R 包 git2r 了解 - 宇飞的世界">


  <link href='/favicon.ico' rel='icon' type='image/x-icon'/>



  








<link href='//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/github.min.css' rel='stylesheet' type='text/css' />



<link rel="stylesheet" href="/css/fonts.css" media="all">
<link rel="stylesheet" href="/css/main.css" media="all">



  </head>
  <body>
    <div class="wrapper">
      <header class="header">
        <nav class="nav">
  <a href="/" class="nav-logo">
    <img src="/images/logo.png"
         width="50"
         height="50"
         alt="Logo">
  </a>

  <ul class="nav-links">
    
    <li><a href="/about/">About</a></li>
    
    <li><a href="/categories/">Categories</a></li>
    
    <li><a href="/tags/">Tags</a></li>
    
    <li><a href="http://www.zhongyufei.com/Rbook/_book">R study</a></li>
    
  </ul>
</nav>

      </header>


<main class="content" role="main">

  <article class="article">
    
    <span class="article-duration">1 min read</span>
    

    <h1 class="article-title">R 包 git2r 了解</h1>

    
    <span class="article-date">2021-06-17</span>
    

    <div class="article-content">
      <h1 id="前言">前言</h1>
<p>git2r 是一个 R 包,主要功能是通过 R 以编程方式访问 Git 存储库.<a href="https://docs.ropensci.org/git2r/index.html">git2r项目地址</a>,如果有需要可以自行浏览官方项目地址学习.本文简单介绍 git2r 的用法,用来拉取或上传数据到 Git 存储库.</p>
<h2 id="安装">安装</h2>
<p>要安装 CRAN 上可用的版本：</p>
<pre><code>install.packages(&quot;git2r&quot;)
</code></pre><p>要安装 的开发版本<code>git2r</code>，最简单的方法是使用 devtools 包：</p>
<pre><code># install.packages(&quot;devtools&quot;)
library(devtools)
install_github(&quot;ropensci/git2r&quot;)
</code></pre><p>另一种选择是使用<code>git</code>和<code>make</code></p>
<pre><code>$ git clone https://github.com/ropensci/git2r.git
$ cd git2r
$ make install
</code></pre><h2 id="用法">用法</h2>
<p>git2r 包中的中心对象是 S3 类 git_repository.以下三种方法可以实例化一个仓库:<code>init</code>,<code>repostitory</code>和<code>clone</code>.</p>
<h3 id="存在仓库">存在仓库</h3>
<pre><code>library(git2r)
#
ls &lt;- cred_user_pass(username = '18520804424',password = 'Vegayf598253220@~!@#$')
repo &lt;- repository(path = getwd())
config(repo)
add(repo = repo,path = list.files(recursive = T,include.dirs = T))
commit(repo,message = 'commit')
push(object = repo,name = 'origin',refspec = 'refs/heads/master',credentials = ls )
</code></pre><h3 id="新建仓库">新建仓库</h3>
<pre><code>library(git2r)
## Create a temporary directory to hold the repository
path &lt;- tempfile(pattern=&quot;git2r-&quot;)
dir.create(path)
## Initialize the repository
repo &lt;- init(path)
## Display a brief summary of the new repository
repo

is_bare(repo)
is_empty(repo)
</code></pre><h3 id="克隆仓库">克隆仓库</h3>
<pre><code>path &lt;- file.path(tempfile(pattern=&quot;git2r-&quot;), &quot;git2r&quot;)
dir.create(path, recursive=TRUE)

## Clone the git2r repository
repo &lt;- clone(&quot;https://github.com/ropensci/git2r&quot;, path)
summary(repo)

references(repo)
branches(repo)
</code></pre>
    </div>
  </article>

  


</main>

      <footer class="footer">
        <ul class="footer-links">
          <li>
            <a href="/index.xml" type="application/rss+xml" target="_blank">RSS feed</a>
          </li>
          <li>
            <a href="https://beian.miit.gov.cn/" target="_blank">粤ICP备20035539号</a>
          </li>
        </ul>
      </footer>

    </div>
    



<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js"></script>



<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/languages/r.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/languages/yaml.min.js"></script>
<script>hljs.configure({languages: []}); hljs.initHighlightingOnLoad();</script>



    
<script src="/js/math-code.js"></script>
<script async src="//cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML"></script>


    
  </body>
</html>

